package middleware

import (
	"OJCenter/global"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/unrolled/secure"
)

func Tls() gin.HandlerFunc {
	return func(c *gin.Context) {
		if global.Conf.HttpConf.ServiceConf.Enable {
			middleware := secure.New(secure.Options{
				SSLRedirect: true,
				SSLHost:     global.Conf.HttpConf.ServiceConf.TlsAddr,
			})
			err := middleware.Process(c.Writer, c.Request)
			if err != nil {
				// 如果出现错误，请不要继续
				fmt.Println(err)
				return
			}
		}
		// 继续往下处理
		c.Next()
	}
}
